<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:check:add']"
        >新增审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:check:edit']"
        >修改</el-button>
      </el-col>
      <div style="float:right;">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="getList"
        >刷新</el-button>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审核状态" align="center" prop="checkStatus" :formatter="checkStatusFormat"/>
      <el-table-column label="审核意见" align="center" prop="checkOpinion" />
      <el-table-column label="审核人" align="center" prop="createByName" />
      <el-table-column label="审核时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row>
            <el-col :span="6">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['crm:check:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="18">
              <el-link :href="scope.row.annex" v-if="scope.row.annex != null" style="color:#1890ff" target="_blank">附件下载</el-link>
            </el-col>
          </el-row>
          
          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否通过" prop="checkStatus">
          <el-select v-model="form.checkStatus" placeholder="请审核">
            <el-option
              v-for="dict1 in checkStatusList"
              :key="dict1.value"
              :label="dict1.name"
              :value="dict1.value"
              ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="checkOpinion">
          <el-input v-model="form.checkOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件" prop="annex">
          <fileUpload v-model="form.annex"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck, exportCheck } from "@/api/crm/check/check";
import FileUpload from '@/components/FileUpload';

export default {
  props: {
      contractId: {},
      checkStatus: {}
  },
  name: "Check",
  components: {
    FileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      //
      checkStatusList: [{value:1,name:"通过"},{value:2,name:"驳回"}],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同审核表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态字典
      checkStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkStatus: [
          { required: true, message: "是否通过不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("check_status").then(response => {
      this.checkStatusOptions = response.data;
    });
  },
  methods: {
    // 审核状态字典翻译
    checkStatusFormat(row, column) {
      return this.selectDictLabel(this.checkStatusOptions, row.checkStatus);
    },
    /** 查询合同审核列表 */
    getList() {
      this.loading = true;
      this.queryParams = {contractId: this.contractId};
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractId: this.contractId,
        checkStatus: null,
        checkOpinion: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        annex: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      if(this.checkStatus == 1){
        this.$alert("该合同已审核通过", "提示", { confirmButtonText: "确定", });  
      }else{
        this.reset();
        this.form.contractId = this.contractId;
        this.open = true;
        this.title = "合同审核";
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheck(id).then(response => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.title = "修改合同审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.checkStatus = this.form.checkStatus;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.checkStatus = this.form.checkStatus;
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合同审核编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCheck(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同审核数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCheck(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  },
    watch: {
        contractId: {
        　　handler(newName, oldName) {
                this.form.contractId = this.contractId;
        　　    if(null != newName){
                    this.getList();
                }
        　　},
            deep: true,
        　　immediate: true
        }
    } 
};
</script>
